#include "stdafx.h"
#include "bbAddon.h"


const float FLOAT_EPSILON = .0001f;
const float SQV2_EPSILON = .0001f;
const float SQV2_EPSILON2= .0001f;

class vector2
{
public :
	vector2(const float x,const float y):_x(x),_y(y){};
	~vector2();
	const bool operator != (const vector2 &v);
	const float operator * (const vector2 &v);
	vector2* operator + (const vector2 &v);
	vector2* operator - (const vector2 &v);
	void operator * (const float f);
	void operator + (const float f);
	void operator - (const float f);

	float _x,_y;
};

vector2::~vector2(){};

inline const bool vector2::operator!=(const vector2 & v){return ((fabs(_x-v._x)<FLOAT_EPSILON)&&(fabs(_y-v._y)<FLOAT_EPSILON));};
inline const float vector2::operator*(const vector2 & v){return (_x*v._y-_y*v._x);};
inline vector2* vector2::operator+(const vector2 & v){return new vector2(_x+v._x,_y+v._y);};
inline vector2* vector2::operator-(const vector2 & v){return new vector2(_x-v._x,_y-v._y);};


BBAEXP const int BBACALL NewVector2(const float x, const float y)
	{return TobbObject(new vector2(x,y));};

BBAEXP const int BBACALL Vector2Equal(vector2 & v1, vector2 & v2){return (v1!=v2);};

BBAEXP void BBACALL SetVector2(vector2 &v, const float x, const float y){v._x=x;v._y=y;};
BBAEXP void BBACALL SetVector2X(vector2 &v, const float x){v._x=x;};
BBAEXP void BBACALL SetVector2Y(vector2 &v, const float y){v._y=y;};

BBAEXP const int BBACALL DeleteVector2(vector2* v){delete v;return 0;};

BBAEXP const float BBACALL Vector2X(const vector2& v){return v._x;};
BBAEXP const float BBACALL Vector2Y(const vector2& v){return v._y;};

BBAEXP const float BBACALL Vector2Mag(const vector2& v){return sqrtf(v._x*v._x+v._y*v._y);};
BBAEXP const float BBACALL Vector2SquareMag(const vector2& v){return (v._x*v._x+v._y*v._y);};

BBAEXP const float BBACALL Vector2Dot(const vector2& v1, const vector2& v2){return (v1._x*v2._x+v1._y*v2._y);};
//BBAEXP const float BBACALL Vector2Cross(vector2& v1, vector2& v2){return (v1*v2);};
BBAEXP const float BBACALL Vector2Cross(vector2& v1, vector2& v2){return (v1._y*v2._x-v1._x*v2._y);};

BBAEXP const float BBACALL MagVector2(vector2& v)
{	float l = Vector2SquareMag(v);
	if (fabs(l-1.0f)>SQV2_EPSILON)
	{
		if (l>SQV2_EPSILON2){l=1.0f/sqrtf(l);v._x*=l;v._y*=l;return 1.0;};
		return 0.0f;
	};
	return 1.0f;
};
BBAEXP const float BBACALL Vector2Angle(const vector2& v1,const vector2& v2){return atan2(v2._y-v1._y,v2._x-v1._x);};
